leisurexi's Blog.

Spring IoC 容器的扩展

字数统计: 5.3k阅读时长: 25 min
2020/06/07 Share

本篇文章主要介绍 Spring 中 BeanFactory 的扩展,也就是 ApplicationContext

前言

本篇文章主要介绍 Spring 中 BeanFactory 的扩展 ApplicationContext,我们平时日常开发中也基本上是使用它,不会去直接使用 BeanFactory

那么在 Spring 中 BeanFactoryApplicationContext 有什么区别呢?

  • BeanFactory 这个接口提供了高级配置的机制的管理对象,是一个基本的 IoC 的容器。
  • ApplicationContextBeanFactory 的一个子接口,提供了 BeanFactory 的全部功能,并且在此基础上还提供了:
    • 面向切面 (AOP)
    • 配置元信息 (Configuration Metadata)
    • 资源管理 (Resources)
    • 事件 (Events)
    • 国际化 (i18n)
    • 注解 (Annotations)
    • Environment 抽象 (Environment Abstraction)

真正的底层 IoC 容器是 BeanFactory 的实现类,ApplicationContext 中的 getBean() 其实都是委托给 DefaultListableBeanFactory 来实现。

正文

首先来看一段使用 ApplicationContext 的简单代码,然后我们逐渐往下分析。

XML配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.leisurexi.ioc.context" />

<bean id="user" class="com.leisurexi.ioc.context.domain.User">
<property name="id" value="1"/>
<property name="name" value="leisurexi"/>
</bean>

<bean id="city" class="com.leisurexi.ioc.context.domain.City">
<property name="id" value="1"/>
<property name="name" value="北京"/>
</bean>

</beans>

User 类定义如下:

1
2
3
4
5
6
7
8
9
10
public class User {

private Long id;
private String name;

@Autowired
private City city;

// 省略get和set方法
}

测试类,如下:

1
2
3
4
5
6
@Test
public void xmlApplicationContextTest() {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}

上面代码很简单,根据指定的文件加载 bean 定义,在调用 getBean() 获取 bean 实例。下面我们从上面代码开始一步一步分析 ApplicationContext 相比 BeanFactory 做了什么其它工作。

ClassPathXmlApplicationContext 构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
}

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
// 设置 XML 文件的路径
setConfigLocations(configLocations);
if (refresh) {
// 刷新上下文
refresh();
}
}

AbstractApplicationContext#refresh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@Override
public void refresh() throws BeansException, IllegalStateException {
// 加锁
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备刷新的上下文环境,见下文详解
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
// 获取刷新后的beanFactory,一般都是创建一个DefaultListableBeanFactory,见下文详解
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
// 使用当前上下文环境准备beanFactory,见下文详解
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
// beanFactory的后置处理,子类实现,这也算是beanFactory的扩展点
// AbstractRefreshableWebApplicationContext在这个方法内加入了request和session的作用域
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
// 调用所有BeanFactoryPostProcessors的实现类,见下文详解
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
// 注册BeanPostProcessors,见下文详解
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
// 初始化消息资源,这里不做过多分析
initMessageSource();

// Initialize event multicaster for this context.
// 初始化事件传播器,这里不做过多分析
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
// 在特殊的上下文环境中初始化指定的bean,模板方法留给子类实现
onRefresh();

// Check for listener beans and register them.
// 注册监听器
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
// 实例化所有非延迟加载的单例bean,见下文详解
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
// 完成上下文的刷新,调用生命周期处理器的onRefresh()并且发布上下文刷新完成事件
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}

// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
// 重置缓存,例如方法、字段等
resetCommonCaches();
}
}
}

AbstractApplicationContext#prepareRefresh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void prepareRefresh() {
// Switch to active.
// 记录开始时间
this.startupDate = System.currentTimeMillis();
// 上下文关闭标识设置为 false
this.closed.set(false);
// 上下文激活标识设置为 true
this.active.set(true);

// Initialize any placeholder property sources in the context environment.
// 初始化占位符属性资源,该方法是留给子类实现的,默认什么也不做
initPropertySources();

// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 验证需要的属性文件是否都已经放入环境中
getEnvironment().validateRequiredProperties();

// Store pre-refresh ApplicationListeners...
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
} else {
// Reset local application listeners to pre-refresh state.
// 在上下文刷新前重置监听器
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}

// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
this.earlyApplicationEvents = new LinkedHashSet<>();
}

AbstractApplicationContext#obtainFreshBeanFactory

1
2
3
4
5
6
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 刷新 bean 工厂,见下文详解
refreshBeanFactory();
// 返回 bean 工厂,见下文详解
return getBeanFactory();
}

AbstractRefreshableApplicationContext#refreshBeanFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
protected final void refreshBeanFactory() throws BeansException {
// 如果有beanFactory
if (hasBeanFactory()) {
// 销毁所有的单例bean
destroyBeans();
// 关闭beanFactory,也就是将beanFactory设置为null
closeBeanFactory();
}
try {
// 创建 DefaultListableBeanFactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
// 指定序列化id
beanFactory.setSerializationId(getId());
// 定制beanFactory,设置相关属性
customizeBeanFactory(beanFactory);
// 加载beanDefinition
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
// 加锁,将beanFactory赋值给全局变量
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}

上面代码中的 destroyBeans() 最终会调用 DefaultListableBeanFactory#destroySingletons() ,该方法在 Spring IoC createBean 方法详解 一文中已经介绍过,这里不再赘述。

loadBeanDefinitions() 方法最终会创建 XmlBeanDefinitionReader#loadBeanDefinitions() 去加载 bean 的定义元信息,该方法在 Spring XML Bean 定义的加载和注册 一文中已经介绍过,这里不再赘述;其中对 context:componment-scan 标签的解析在 Spring IoC component-scan 节点详解 一文中介绍过,这里也不再赘述。

AbstractRefreshableApplicationContext#getBeanFactory

1
2
3
4
5
6
7
8
9
10
11
public final ConfigurableListableBeanFactory getBeanFactory() {
// 加锁
synchronized (this.beanFactoryMonitor) {
// 如果beanFactory为空抛出异常
if (this.beanFactory == null) {
throw new IllegalStateException("BeanFactory not initialized or already closed - " + "call 'refresh' before accessing beans via the ApplicationContext");
}
// 返回beanFactory
return this.beanFactory;
}
}

AbstractApplicationContext#prepareBeanFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
// 设置beanFactory的classLoader为当前context的classLoader
beanFactory.setBeanClassLoader(getClassLoader());
// 设置beanFactory的表达式语言处理器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 为beanFactory增加了一个的propertyEditor,这个主要是对bean的属性等设置管理的一个工具
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

// Configure the bean factory with context callbacks.
// 添加bean扩展,主要是对ApplicationContext新增加的Aware接口进行调用
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

// 设置几个忽略自动装配的接口
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
// 注册解决依赖,也就是说我们可以通过依赖注入来注入以下四种类型的bean
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);

// Register early post-processor for detecting inner beans as ApplicationListeners.
// 将是ApplicationListener类型的bean在BeanPostProcessor的初始化后回调方法中加入到context的监听器列表中
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

// Detect a LoadTimeWeaver and prepare for weaving, if found.
// 增加对AspectJ支持
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}

// Register default environment beans.
// 如果beanFactory不存在名为environment的bean,添加默认的,该bean就和我们正常声明的单例bean一样
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
// 如果beanFactory不存在名为systemProperties的bean,添加默认的,该bean就和我们正常声明的单例bean一样
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
// 如果systemEnvironment不存在名为systemEnvironment的bean,添加默认的,该bean就和我们正常声明的单例bean一样
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}

AbstractApplicationContext#invokeBeanFactoryPostProcessors

1
2
3
4
5
6
7
8
9
10
11
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
// 实例化并调用所有已注册的BeanFactoryPostProcessor
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}

PostProcessorRegistryDelegate#invokeBeanFactoryPostProcessors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<>();
// 判断beanFactory是否是BeanDefinitionRegistry类型,通常情况下这里的beanFactory是DefaultListableBeanFactory所以这里判断为true
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// 保存实现了BeanFactoryPostProcessor bean的集合
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
// 保存实现了BeanDefinitionRegistryPostProcessor bean的集合
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
// 遍历beanFactoryPostProcessors
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
// 找出是BeanDefinitionRegistryPostProcessor类型的并调用其postProcessBeanDefinitionRegistry()
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
// 将BeanDefinitionRegistryPostProcessor类型的添加进registryProcessors
registryProcessors.add(registryProcessor);
}
else {
// 将BeanFactoryPostProcessor类型的添加进regularPostProcessors
regularPostProcessors.add(postProcessor);
}
}

// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
// 获取所有BeanDefinitionRegistryPostProcessor类型的beanName
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// 遍历postProcessorNames
for (String ppName : postProcessorNames) {
// 如果实现了PriorityOrdered接口,
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
// 初始化此bean并添加进currentRegistryProcessors
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
// 将此beanName添加到已处理的记录中
processedBeans.add(ppName);
}
}
// 排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
// 将所有BeanDefinitionRegistryPostProcessor类型并且实现了PriorityOrdered接口的bean添加进registryProcessors
registryProcessors.addAll(currentRegistryProcessors);
// 遍历调用currentRegistryProcessors中的所有BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry()
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// 清空currentRegistryProcessors
currentRegistryProcessors.clear();

// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// 和上面的差不多只是这次是实现了Ordered接口的,并且没有处理过的
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();

// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// 和上面的差不多只是这次是所有的实现了BeanDefinitionRegistryPostProcessors的bean,并且没有处理过的
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}

// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
// 调用BeanFactoryPostProcessor的postProcessBeanFactory()
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}

else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}

// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// 获取所有BeanFactoryPostProcessor类型的beanName
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
// 遍历postProcessorNames
for (String ppName : postProcessorNames) {
// 如果已经处理过,直接跳过;因为BeanDefinitionRegistryPostProcessor继承于BeanFactoryPostProcessor
// 所以postProcessorNames也包含BeanDefinitionRegistryPostProcessor类型的bean,这里会对BeanDefinitionRegistryPostProcessor类型的bean直接跳过
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
// 如果实现了PriorityOrdered接口,初始化该bean并添加进priorityOrderedPostProcessors
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
// 如果实现了Ordered接口,将beanName添加进orderedPostProcessorNames
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
// 正常的将beanName添加进nonOrderedPostProcessorNames
else {
nonOrderedPostProcessorNames.add(ppName);
}
}

// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
// 排序,然后调用BeanFactoryPostProcessors的postProcessBeanFactory()
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
// 和上面的一样这里是实现了Ordered接口的
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

// Finally, invoke all other BeanFactoryPostProcessors.
// 和上面的一样这里是正常的BeanFactoryPostProcessors
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

// Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders in values...
beanFactory.clearMetadataCache();
}

上面代码首先找出 BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor 类型的 bean,然后根据其实现的排序接口,来分别进行初始化以及调用其回调方法。可以把 PriorityOrdered 理解为 超级会员Ordered普通会员,都未实现的理解为 普通用户,优先级一个比一个高。

我们首先看一下 BeanFactoryPostProcessor 接口的定义:

1
2
3
4
5
6
7
8
9
@FunctionalInterface
public interface BeanFactoryPostProcessor {

/**
* 实例化bean之前调用,可以在此修改BeanDefinition
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

可以看出 BeanFactoryPostProcessor 接口是 Spring 初始化 BeanFactory 时对外暴露的扩展点,Spring IoC 容器允许 BeanFactoryPostProcessor 在容器实例化任何 bean 之前读取 bean 的定义,并可以修改它。

接下里我们看一下 BeanDefinitionRegistryPostProcessor 接口的定义:

1
2
3
4
5
6
7
8
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

/**
* 实例化bean之前调用,可以在此注册bean或删除bean
*/
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor 具有更高的优先级,从上面解析的代码中就可以看出,主要用来在 BeanFactoryPostProcessor 之前注册其它 bean 的定义。

AbstractApplicationContext#registerBeanPostProcessors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

//PostProcessorRegistrationDelegate.java
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 获取所有实现了BeanPostProcessor的beanName,这里会获取到AutowiredAnnotationProcessor和CommonAnnotationProcessor后置处理器的beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
// 已经注册进beanFactory的数量 + 手动注册的BeanPostProcessorChecker + 实现了BeanPostProcessor还未注册的bean的数量
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
// 存储实现了PriorityOrdered接口的BeanPostProcessors
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
// 存储实现了MergedBeanDefinitionPostProcessor接口的BeanPostProcessors
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
// 存储实现了Ordered接口的BeanPostProcessors
List<String> orderedPostProcessorNames = new ArrayList<>();
// 存储正常的BeanPostProcessors
List<String> nonOrderedPostProcessorNames = new ArrayList<>();

for (String ppName : postProcessorNames) {
// 如果实现了BeanPostProcessor的bean实现了PriorityOrdered接口
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
// 获取bean实例
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
// 添加进priorityOrderedPostProcessors
priorityOrderedPostProcessors.add(pp);
// 如果bean也实现了MergedBeanDefinitionPostProcessor,则添加进internalPostProcessors
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
// 如果实现了Ordered接口,添加进orderedPostProcessorNames
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
// 否则添加进nonOrderedPostProcessorNames
else {
nonOrderedPostProcessorNames.add(ppName);
}
}

// First, register the BeanPostProcessors that implement PriorityOrdered.
// 将实现了PriorityOrdered的BeanPostProcessors先排序再注册进beanFactory
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

// Next, register the BeanPostProcessors that implement Ordered.
// 将实现了Order的BeanPostProcessors先排序再注册进beanFactory
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
// 如果实现了MergedBeanDefinitionPostProcessor
if (pp instanceof MergedBeanDefinitionPostProcessor) {
// 添加进internalPostProcessors
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);

// Now, register all regular BeanPostProcessors.
// 将正常的BeanPostProcessors注册进beanFactory
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

// Finally, re-register all internal BeanPostProcessors.
// 最后将实现MergedBeanDefinitionPostProcessor的BeanPostProcessors先排序再注册进beanFactory
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);

// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
// 这里再次添加了ApplicationListenerDetector(之前在prepareBeanFactory()已经添加过)是为了获取代理
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

上面代码最后的部分会把实现了 MergedBeanDefinitionPostProcessor 会在最后重新注册一遍,大家可能会认为这不就重复注册了吗,其实不然,beanFactory#addBeanPostProcessor() 会首先删除老的,再重新添加新的。

根据上面代码大家也会发现,ApplicationContext 会帮我们自动注册实现了 BeanPostProcessorsbean,而使用 BeanFactory 就需要自己手动注册了。

AbstractApplicationContext#finishBeanFactoryInitialization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}

// Register a default embedded value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}

// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}

// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);

// Allow for caching all bean definition metadata, not expecting further changes.
// 冻结所有的bean定义,也就是bean定义将不被修改或任何进一步的处理
beanFactory.freezeConfiguration();

// Instantiate all remaining (non-lazy-init) singletons.
// 初始化非延迟的单例bean,见下文详解
beanFactory.preInstantiateSingletons();
}

DefaultListableBeanFactory#preInstantiateSingletons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("Pre-instantiating singletons in " + this);
}

// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
// 获取合并的BeanDefinition
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// bean不是抽象类 && bean是单例作用域 && bean不是延迟加载
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
// 如果bean的FactoryBean
if (isFactoryBean(beanName)) {
// 获取FactoryBean的实例,前面加了&符号
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext());
}
else {
// FactoryBean是否提前初始化
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
// 如果是提前初始化直接调用getBean()去初始化bean
if (isEagerInit) {
getBean(beanName);
}
}
}
// 直接调用getBean()去初始化bean
else {
getBean(beanName);
}
}
}

// Trigger post-initialization callback for all applicable beans...
for (String beanName : beanNames) {
// 获取上面初始化后的单例bean
Object singletonInstance = getSingleton(beanName);
// 如果bean实现了SmartInitializingSingleton接口,调用afterSingletonsInstantiated()
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}

上面代码中的 getMergedLocalBeanDefinition() 在[Spring IoC getBean 方法详解](https://leisurexi.github.io/category/2020/04/21/Spring IoC/Spring IoC getBean 方法详解.html)有解析过,这里不再赘述。这里介绍一下 SmartInitializingSingleton 接口,先看下该接口的定义:

1
2
3
4
5
6
7
8
public interface SmartInitializingSingleton {

/**
* 单例bean初始化完成之后调用
*/
void afterSingletonsInstantiated();

}

这个接口比较简单,就一个方法,并且只在 preInstantiateSingletons() 中调用了,也就是说你直接使用 BeanFactory 是不会调用该回调方法的。该接口回调方法在单例 bean 初始化完成之后调用后执行,属于 Spring Bean 生命周期的增强。

AbstractApplicationContext#finishRefresh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
// 清除资源缓存
clearResourceCaches();

// Initialize lifecycle processor for this context.
// 为此上下文初始化生命周期处理器,见下文详解
initLifecycleProcessor();

// Propagate refresh to lifecycle processor first.
// 首先将刷新完毕事件传播到生命周期处理器,见下详解
getLifecycleProcessor().onRefresh();

// Publish the final event.
// 发布上下文刷新完成的事件
publishEvent(new ContextRefreshedEvent(this));

// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}

AbstractApplicationContext#initLifecycleProcessor

1
2
3
4
5
6
7
8
9
10
11
12
13
protected void initLifecycleProcessor() {
// 如果当前beanFactory中含有名称为lifecycleProcessor的bean定义,初始化该bean并赋值给全局变量lifecycleProcessor
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
this.lifecycleProcessor = beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
} else {
// beanFactory中没有名称为lifecycleProcessor的bean定义,创建一个DefaultLifecycleProcessor并当做单例bean注册进beanFactory
DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
defaultProcessor.setBeanFactory(beanFactory);
this.lifecycleProcessor = defaultProcessor;
beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
}
}

DefaultLifecycleProcessor#onRefresh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void onRefresh() {
startBeans(true);
this.running = true;
}

private void startBeans(boolean autoStartupOnly) {
// 获取所有实现了Lifecycle或者SmartLifecycle的单例bean
Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
Map<Integer, LifecycleGroup> phases = new HashMap<>();
// 因为onRefresh()调用时该方法时,手动设置了autoStartupOnly为false,所以这里的bean必需是SmartLifecycle类型并且isAutoStartup()返回true
lifecycleBeans.forEach((beanName, bean) -> {
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
// 获取bean的阶段值(如果没有实现Phased接口,则值为0)
int phase = getPhase(bean);
// 拿到存放该阶段值的LifecycleGroup,如果为空则新建一个并把当前阶段值加入其中
LifecycleGroup group = phases.get(phase);
if (group == null) {
group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
phases.put(phase, group);
}
group.add(beanName, bean);
}
});
// 如果phases不为空,根据阶段值从小到大排序,并调用重写Lifecycle接口的start()
if (!phases.isEmpty()) {
List<Integer> keys = new ArrayList<>(phases.keySet());
Collections.sort(keys);
for (Integer key : keys) {
phases.get(key).start();
}
}
}

总结

本文主要介绍了 ApplicationContext 整个加载的流程,我们可以重新整理一下思路:

  1. 刷新前的准备,在这里会记录整个上下文启动的开始时间,将激活标识设置为 true,关闭标识设置为 false
  2. 创建一个新的 BeanFactory,这里大多数情况下都是 DefaultListableBeanFactory。首先会检测之前有没有 BeanFactory,有的话会先销毁再重新创建,然后会加载 bean 的定义元信息。
  3. 配置 BeanFactory,设置 BeanFactoryclassLoader、表达式语言处理器、添加了 ApplicationContext 新增加的 Aware 接口回调等。
  4. 调用 BeanFactory 的后置处理器,这也是 BeanFactory 的扩展点;上文有分析过这里不再赘述。
  5. 注册容器内所有的 BeanPostProcessors,上文也分析过,不再赘述;值得注意的是如果单单使用 BeanFactory 的话是不会自动注册的。
  6. 初始化消息资源,这里没有过多分析,因为对我们整个流程几乎没什么影响。
  7. 初始化事件传播器。关于 Spring 的事件,我打算后面单独写一篇文章来介绍,这里就没有多说。
  8. 在特殊的上下文环境中初始化指定的bean,模板方法留给子类实现。
  9. 注册监听器,这也留着到 Spring 事件中一起介绍。
  10. 初始化所有非延迟加载的单例 bean,并且会回调实现了 SmartInitializingSingleton 接口的 afterSingletonsInstantiated(),这个接口算是 bean 生命周期的增强。
  11. 完成上下文的刷新,调用生命周期处理器的 onRefresh() 并且发布上下文刷新完成事件。

最后,我模仿 Spring 写了一个精简版,代码会持续更新。地址:https://github.com/leisurexi/tiny-spring。访问新博客地址,观看效果更佳 https://leisurexi.github.io/

CATALOG
  1. 1. 前言
  2. 2. 正文
    1. 2.1. ClassPathXmlApplicationContext 构造函数
    2. 2.2. AbstractApplicationContext#refresh
    3. 2.3. AbstractApplicationContext#prepareRefresh
    4. 2.4. AbstractApplicationContext#obtainFreshBeanFactory
    5. 2.5. AbstractRefreshableApplicationContext#refreshBeanFactory
    6. 2.6. AbstractRefreshableApplicationContext#getBeanFactory
    7. 2.7. AbstractApplicationContext#prepareBeanFactory
    8. 2.8. AbstractApplicationContext#invokeBeanFactoryPostProcessors
    9. 2.9. PostProcessorRegistryDelegate#invokeBeanFactoryPostProcessors
    10. 2.10. AbstractApplicationContext#registerBeanPostProcessors
    11. 2.11. AbstractApplicationContext#finishBeanFactoryInitialization
    12. 2.12. DefaultListableBeanFactory#preInstantiateSingletons
    13. 2.13. AbstractApplicationContext#finishRefresh
    14. 2.14. AbstractApplicationContext#initLifecycleProcessor
    15. 2.15. DefaultLifecycleProcessor#onRefresh
  3. 3. 总结